import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**玩家信息属性枚举*/
export const enum E_PlayerInfo {
	/**创建时间*/
	CREATE_TIME = 0,
	/**上次登陆时间*/
	LAST_LOGIN_TIME = 1,
	/**上次下线时间*/
	LAST_LOGOUT_TIME = 2,
	/**登陆时间*/
	LOGIN_TIME = 3,
	/**昵称*/
	NICKNAME = 6,
	/**职业*/
	PROFESSION = 7,
	/**等级*/
	LEVEL = 8,
	/**头像索引*/
	HEAD_ICON = 9,
	/**上次修改头像时间*/
	LAST_SET_NAME_TIME = 10,
	/**选择角色*/
	SELECT_ROLE = 11,
	/**改名次数*/
	SET_NAME_COUNT = 12,
	/**战力*/
	FIGHT_SCORE = 13,
	/**枚举数量*/
	EnumCount = 14
}


/**玩家信息*/
export class PlayerInfo {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_PLAYER_INFO_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_PLAYER_INFO_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: PlayerInfo[] = [];

	/**创建入口*/
	public static create(json?: string): PlayerInfo {
		const c = this.POOL.pop() || new PlayerInfo();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**创建时间 (long)*/
	protected _createTime: number;
	/**上次登陆时间 (long)*/
	protected _lastLoginTime: number;
	/**上次下线时间 (long)*/
	protected _lastLogoutTime: number;
	/**登陆时间 (long)*/
	protected _loginTime: number;
	/**昵称*/
	protected _nickname: string;
	/**职业 (uint8)*/
	protected _profession: number;
	/**等级 (uint32)*/
	protected _level: number;
	/**头像索引 (uint8)*/
	protected _headIcon: number;
	/**上次修改头像时间 (long)*/
	protected _lastSetNameTime: number;
	/**选择角色 (uint8)*/
	protected _selectRole: number;
	/**改名次数 (uint16)*/
	protected _setNameCount: number;
	/**战力 (long)*/
	protected _fightScore: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (PlayerInfo.POOL.indexOf(this) === -1) {
			PlayerInfo.POOL.push(this);
		}
	}

	/**创建时间*/
	public get createTime() {
		return this._createTime;
	}


	/**创建时间*/
	public set createTime(v: number) {
		this._createTime = v;
	}


	/**上次登陆时间*/
	public get lastLoginTime() {
		return this._lastLoginTime;
	}


	/**上次登陆时间*/
	public set lastLoginTime(v: number) {
		this._lastLoginTime = v;
	}


	/**上次下线时间*/
	public get lastLogoutTime() {
		return this._lastLogoutTime;
	}


	/**上次下线时间*/
	public set lastLogoutTime(v: number) {
		this._lastLogoutTime = v;
	}


	/**登陆时间*/
	public get loginTime() {
		return this._loginTime;
	}


	/**登陆时间*/
	public set loginTime(v: number) {
		this._loginTime = v;
	}


	/**昵称*/
	public get nickname() {
		return this._nickname;
	}


	/**昵称*/
	public set nickname(v: string) {
		this._nickname = v;
	}


	/**职业*/
	public get profession() {
		return this._profession;
	}


	/**职业*/
	public set profession(v: number) {
		this._profession = v;
	}


	/**等级*/
	public get level() {
		return this._level;
	}


	/**等级*/
	public set level(v: number) {
		this._level = v;
	}


	/**头像索引*/
	public get headIcon() {
		return this._headIcon;
	}


	/**头像索引*/
	public set headIcon(v: number) {
		this._headIcon = v;
	}


	/**上次修改头像时间*/
	public get lastSetNameTime() {
		return this._lastSetNameTime;
	}


	/**上次修改头像时间*/
	public set lastSetNameTime(v: number) {
		this._lastSetNameTime = v;
	}


	/**选择角色*/
	public get selectRole() {
		return this._selectRole;
	}


	/**选择角色*/
	public set selectRole(v: number) {
		this._selectRole = v;
	}


	/**改名次数*/
	public get setNameCount() {
		return this._setNameCount;
	}


	/**改名次数*/
	public set setNameCount(v: number) {
		this._setNameCount = v;
	}


	/**战力*/
	public get fightScore() {
		return this._fightScore;
	}


	/**战力*/
	public set fightScore(v: number) {
		this._fightScore = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_PlayerInfo): any {
		switch (k) {
			case E_PlayerInfo.CREATE_TIME: return this._createTime;
			case E_PlayerInfo.LAST_LOGIN_TIME: return this._lastLoginTime;
			case E_PlayerInfo.LAST_LOGOUT_TIME: return this._lastLogoutTime;
			case E_PlayerInfo.LOGIN_TIME: return this._loginTime;
			case E_PlayerInfo.NICKNAME: return this._nickname;
			case E_PlayerInfo.PROFESSION: return this._profession;
			case E_PlayerInfo.LEVEL: return this._level;
			case E_PlayerInfo.HEAD_ICON: return this._headIcon;
			case E_PlayerInfo.LAST_SET_NAME_TIME: return this._lastSetNameTime;
			case E_PlayerInfo.SELECT_ROLE: return this._selectRole;
			case E_PlayerInfo.SET_NAME_COUNT: return this._setNameCount;
			case E_PlayerInfo.FIGHT_SCORE: return this._fightScore;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_PlayerInfo, v: any): void {
		switch (k) {
			case E_PlayerInfo.CREATE_TIME: this._createTime = v; break;
			case E_PlayerInfo.LAST_LOGIN_TIME: this._lastLoginTime = v; break;
			case E_PlayerInfo.LAST_LOGOUT_TIME: this._lastLogoutTime = v; break;
			case E_PlayerInfo.LOGIN_TIME: this._loginTime = v; break;
			case E_PlayerInfo.NICKNAME: this._nickname = v; break;
			case E_PlayerInfo.PROFESSION: this._profession = v; break;
			case E_PlayerInfo.LEVEL: this._level = v; break;
			case E_PlayerInfo.HEAD_ICON: this._headIcon = v; break;
			case E_PlayerInfo.LAST_SET_NAME_TIME: this._lastSetNameTime = v; break;
			case E_PlayerInfo.SELECT_ROLE: this._selectRole = v; break;
			case E_PlayerInfo.SET_NAME_COUNT: this._setNameCount = v; break;
			case E_PlayerInfo.FIGHT_SCORE: this._fightScore = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//创建时间
		this._createTime = 0;
		//上次登陆时间
		this._lastLoginTime = 0;
		//上次下线时间
		this._lastLogoutTime = 0;
		//登陆时间
		this._loginTime = 0;
		//昵称
		this._nickname = '';
		//职业
		this._profession = 0;
		//等级
		this._level = 0;
		//头像索引
		this._headIcon = 0;
		//上次修改头像时间
		this._lastSetNameTime = 0;
		//选择角色
		this._selectRole = 0;
		//改名次数
		this._setNameCount = 0;
		//战力
		this._fightScore = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._createTime) {
			json.createTime = this._createTime;
		}
		if (this._lastLoginTime) {
			json.lastLoginTime = this._lastLoginTime;
		}
		if (this._lastLogoutTime) {
			json.lastLogoutTime = this._lastLogoutTime;
		}
		if (this._loginTime) {
			json.loginTime = this._loginTime;
		}
		if (this._nickname) {
			json.nickname = this._nickname;
		}
		if (this._profession) {
			json.profession = this._profession;
		}
		if (this._level) {
			json.level = this._level;
		}
		if (this._headIcon) {
			json.headIcon = this._headIcon;
		}
		if (this._lastSetNameTime) {
			json.lastSetNameTime = this._lastSetNameTime;
		}
		if (this._selectRole) {
			json.selectRole = this._selectRole;
		}
		if (this._setNameCount) {
			json.setNameCount = this._setNameCount;
		}
		if (this._fightScore) {
			json.fightScore = this._fightScore;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.createTime) {
			this._createTime = json.createTime;
		}
		if (json.lastLoginTime) {
			this._lastLoginTime = json.lastLoginTime;
		}
		if (json.lastLogoutTime) {
			this._lastLogoutTime = json.lastLogoutTime;
		}
		if (json.loginTime) {
			this._loginTime = json.loginTime;
		}
		if (json.nickname) {
			this._nickname = json.nickname;
		}
		if (json.profession) {
			this._profession = json.profession;
		}
		if (json.level) {
			this._level = json.level;
		}
		if (json.headIcon) {
			this._headIcon = json.headIcon;
		}
		if (json.lastSetNameTime) {
			this._lastSetNameTime = json.lastSetNameTime;
		}
		if (json.selectRole) {
			this._selectRole = json.selectRole;
		}
		if (json.setNameCount) {
			this._setNameCount = json.setNameCount;
		}
		if (json.fightScore) {
			this._fightScore = json.fightScore;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeLongNumber(this._createTime);
		byte.writeLongNumber(this._lastLoginTime);
		byte.writeLongNumber(this._lastLogoutTime);
		byte.writeLongNumber(this._loginTime);
		byte.writeString(this._nickname);
		byte.writeUint8(this._profession);
		byte.writeUint32(this._level);
		byte.writeUint8(this._headIcon);
		byte.writeLongNumber(this._lastSetNameTime);
		byte.writeUint8(this._selectRole);
		byte.writeUint16(this._setNameCount);
		byte.writeLongNumber(this._fightScore);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._createTime = byte.readLongNumber();
		this._lastLoginTime = byte.readLongNumber();
		this._lastLogoutTime = byte.readLongNumber();
		this._loginTime = byte.readLongNumber();
		this._nickname = byte.readString();
		this._profession = byte.readUint8();
		this._level = byte.readUint32();
		this._headIcon = byte.readUint8();
		this._lastSetNameTime = byte.readLongNumber();
		this._selectRole = byte.readUint8();
		this._setNameCount = byte.readUint16();
		this._fightScore = byte.readLongNumber();
		return this;
	}


	/**克隆*/
	public clone(): PlayerInfo {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = PlayerInfo.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = PlayerInfo.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//创建时间
			if (idx === E_PlayerInfo.CREATE_TIME) {
				const oldv = this._createTime;
				this._createTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._createTime, oldv);
				i += 2;
				continue;
			}
			//上次登陆时间
			if (idx === E_PlayerInfo.LAST_LOGIN_TIME) {
				const oldv = this._lastLoginTime;
				this._lastLoginTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._lastLoginTime, oldv);
				i += 2;
				continue;
			}
			//上次下线时间
			if (idx === E_PlayerInfo.LAST_LOGOUT_TIME) {
				const oldv = this._lastLogoutTime;
				this._lastLogoutTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._lastLogoutTime, oldv);
				i += 2;
				continue;
			}
			//登陆时间
			if (idx === E_PlayerInfo.LOGIN_TIME) {
				const oldv = this._loginTime;
				this._loginTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._loginTime, oldv);
				i += 2;
				continue;
			}
			//昵称
			if (idx === E_PlayerInfo.NICKNAME) {
				const oldv = this._nickname;
				this._nickname = us[i + 1];
				IAsync.dispatch(eventID, idx, this._nickname, oldv);
				i += 2;
				continue;
			}
			//职业
			if (idx === E_PlayerInfo.PROFESSION) {
				const oldv = this._profession;
				this._profession = us[i + 1];
				IAsync.dispatch(eventID, idx, this._profession, oldv);
				i += 2;
				continue;
			}
			//等级
			if (idx === E_PlayerInfo.LEVEL) {
				const oldv = this._level;
				this._level = us[i + 1];
				IAsync.dispatch(eventID, idx, this._level, oldv);
				i += 2;
				continue;
			}
			//头像索引
			if (idx === E_PlayerInfo.HEAD_ICON) {
				const oldv = this._headIcon;
				this._headIcon = us[i + 1];
				IAsync.dispatch(eventID, idx, this._headIcon, oldv);
				i += 2;
				continue;
			}
			//上次修改头像时间
			if (idx === E_PlayerInfo.LAST_SET_NAME_TIME) {
				const oldv = this._lastSetNameTime;
				this._lastSetNameTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._lastSetNameTime, oldv);
				i += 2;
				continue;
			}
			//选择角色
			if (idx === E_PlayerInfo.SELECT_ROLE) {
				const oldv = this._selectRole;
				this._selectRole = us[i + 1];
				IAsync.dispatch(eventID, idx, this._selectRole, oldv);
				i += 2;
				continue;
			}
			//改名次数
			if (idx === E_PlayerInfo.SET_NAME_COUNT) {
				const oldv = this._setNameCount;
				this._setNameCount = us[i + 1];
				IAsync.dispatch(eventID, idx, this._setNameCount, oldv);
				i += 2;
				continue;
			}
			//战力
			if (idx === E_PlayerInfo.FIGHT_SCORE) {
				const oldv = this._fightScore;
				this._fightScore = us[i + 1];
				IAsync.dispatch(eventID, idx, this._fightScore, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in PlayerInfo with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: PlayerInfo): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._createTime !== other._createTime) {
			updates.push(E_PlayerInfo.CREATE_TIME, this._createTime);
		}
		if (this._lastLoginTime !== other._lastLoginTime) {
			updates.push(E_PlayerInfo.LAST_LOGIN_TIME, this._lastLoginTime);
		}
		if (this._lastLogoutTime !== other._lastLogoutTime) {
			updates.push(E_PlayerInfo.LAST_LOGOUT_TIME, this._lastLogoutTime);
		}
		if (this._loginTime !== other._loginTime) {
			updates.push(E_PlayerInfo.LOGIN_TIME, this._loginTime);
		}
		if (this._nickname !== other._nickname) {
			updates.push(E_PlayerInfo.NICKNAME, this._nickname);
		}
		if (this._profession !== other._profession) {
			updates.push(E_PlayerInfo.PROFESSION, this._profession);
		}
		if (this._level !== other._level) {
			updates.push(E_PlayerInfo.LEVEL, this._level);
		}
		if (this._headIcon !== other._headIcon) {
			updates.push(E_PlayerInfo.HEAD_ICON, this._headIcon);
		}
		if (this._lastSetNameTime !== other._lastSetNameTime) {
			updates.push(E_PlayerInfo.LAST_SET_NAME_TIME, this._lastSetNameTime);
		}
		if (this._selectRole !== other._selectRole) {
			updates.push(E_PlayerInfo.SELECT_ROLE, this._selectRole);
		}
		if (this._setNameCount !== other._setNameCount) {
			updates.push(E_PlayerInfo.SET_NAME_COUNT, this._setNameCount);
		}
		if (this._fightScore !== other._fightScore) {
			updates.push(E_PlayerInfo.FIGHT_SCORE, this._fightScore);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(PlayerInfo.TOTAL_EVENT_ID, this.getV(0));
	}

}
